#include "hw.h"
#include "ui_hw.h"

hw::hw(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::hw)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    clkTimer=new QTimer(this);
    connect(clkTimer,SIGNAL(timeout()),this,SLOT(SystemClkUpdate()));
    clkTimer->start(60*1000);
    SystemClkUpdate();
    readywork=NULL;
    aboutme=NULL;

#if BOARD
    Api_Work_Init((void *)this,callback);
#endif
}

hw::~hw()
{
#if BOARD
   Api_Work_Uninit();
#endif
    if(readywork)
        delete readywork;
    delete ui;
}

#if BOARD
int hw::callback(void *device, CALLBACK_MSG msg)
{
    hw *tmp;
    tmp=(hw *)device;
    switch(msg.type){
        case MSG_TEMPER:
            tmp->ui->lblTemper->setText(tr("温度:")+QString::number(msg.value)+QString("℃"));
            break;
        case MSG_HUMI:
            tmp->ui->lblHumi->setText(tr("湿度:")+QString::number(msg.value)+QString("%"));
            break;
        case MSG_WORK_STATE:
            switch((WORK_STATE)msg.value){
                case STATE_NOT_READY:
                    tmp->ui->btnWork->setText(tr("错误"));
                    tmp->ui->btnWork->setEnabled(false);
                    tmp->ui->lblWorkState->setText(tr("系统错误！"));
                    break;
                case STATE_READY:
                    tmp->ui->btnWork->setText(tr("准备"));
                    tmp->ui->btnWork->setEnabled(true);
                    tmp->ui->lblWorkState->setText(tr("一切正常！"));
                    break;
                case STATE_WORKING:
                    tmp->ui->btnWork->setText(tr("停止"));
                    tmp->ui->btnWork->setEnabled(true);
                    break;
                case STATE_FINISHED:{
                    QMessageBox msgBox;
                    tmp->ui->btnWork->setText(tr("完成"));
                    tmp->ui->btnWork->setEnabled(false);
                    tmp->ui->lblWorkState->setText(tr("任务完成,请稍候！"));
                    msgBox.setIconPixmap(QPixmap(QString(":/icos/res/ico/msgbox_ok.png")));
                    msgBox.addButton(tr("确定"),QMessageBox::AcceptRole);
                    msgBox.setText(tr("任务完成,系统将继续运行30秒,请稍候！"));
                    msgBox.exec();
                    }
                    break;
                default:
                    break;
            }
            break;
        case MSG_WORK_TIME:{
                int day=0,hour=0,mins=0;
                QString sday,shour,smins;
                day=(int)msg.value/(24*60);
                hour=((int)msg.value-day*(24*60))/60;
                mins=(int)msg.value-day*(24*60)-hour*60;
                if(day)
                    sday=QString::number(day)+tr("天");
                if(hour||day)
                    shour=QString::number(hour)+tr("小时");
                smins=QString::number(mins)+tr("分钟");
                tmp->ui->lblWorkState->setText(tr("工作时长:")+sday+shour+smins);
            }
            break;
        case MSG_WORK_INIT_STEP:
            break;
        default:
            break;
    }
    return 1;
}
#endif

void hw::on_btnWork_clicked()
{
#if BOARD
    if(Api_Work_Get_WorkState()==STATE_READY){
        if(readywork==NULL)
            readywork=new ReadyWork(this);
        disconnect(readywork,0,0,0);
        connect(readywork,SIGNAL(isReady(float,float,unsigned long long)),this,SLOT(StartWork(float,float,unsigned long long)));
        readywork->show();
    }
    else if(Api_Work_Get_WorkState()==STATE_WORKING){
            QMessageBox msgBox;
            int ret;
            msgBox.setIconPixmap(QPixmap(QString(":/icos/res/ico/msgbox_error.png")));
            msgBox.addButton(tr("确定"),QMessageBox::AcceptRole);
            msgBox.addButton(tr("取消"),QMessageBox::RejectRole);
            msgBox.setText(tr("任务还未完成，是否结束当前任务？"));
            ret=msgBox.exec();
            if(!ret)
                Api_Work_Stop();
    }
#else
    QMessageBox *msgBox=new QMessageBox(this);
    msgBox->setIconPixmap(QPixmap(QString(":/icos/res/ico/msgbox_ok.png")));
    msgBox->addButton(tr("确定"),QMessageBox::AcceptRole);
    msgBox->setText(tr("这除vzxcz二位"));
 //   msgBox->setWindowFlags(Qt::FramelessWindowHint);
    printf("aaaaa:%d\n",msgBox->exec());
    if(readywork==NULL)
        readywork=new ReadyWork(this);
    readywork->show();
#endif
}

void hw::StartWork(float temper,float humi,U64 worktime)
{
#if BOARD
    Api_Work_Start(temper,humi,worktime);
#endif
}

void hw::on_btnFunc_Fan_clicked()
{
#if BOARD
    if(Api_Work_Get_CtrbState().s_fan==SWITCH_ON){
        Api_Work_CtrbCmd_Fan(CTRB_SWITCH_OFF);
        ui->btnFunc_Fan->setIcon(QIcon(":/icos/res/ico/fan_off.png"));
    }
    else{
        Api_Work_CtrbCmd_Fan(CTRB_SWITCH_ON);
        ui->btnFunc_Fan->setIcon(QIcon(":/icos/res/ico/fan_on.png"));
    }
#endif
}

void hw::on_btnFunc_Light1_clicked()
{
#if BOARD
    if(Api_Work_Get_CtrbState().s_light1==SWITCH_ON){
        Api_Work_CtrbCmd_Light1(CTRB_SWITCH_OFF);
        ui->btnFunc_Light1->setIcon(QIcon(":/icos/res/ico/light_off.png"));
    }
    else{
        Api_Work_CtrbCmd_Light1(CTRB_SWITCH_ON);
        ui->btnFunc_Light1->setIcon(QIcon(":/icos/res/ico/light_on.png"));
    }
#endif
}

void hw::on_btnFunc_Light2_clicked()
{
#if BOARD
    if(Api_Work_Get_CtrbState().s_light2==SWITCH_ON){
        Api_Work_CtrbCmd_Light2(CTRB_SWITCH_OFF);
        ui->btnFunc_Light1->setIcon(QIcon(":/icos/res/ico/light_off.png"));
    }
    else{
        Api_Work_CtrbCmd_Light2(CTRB_SWITCH_ON);
        ui->btnFunc_Light1->setIcon(QIcon(":/icos/res/ico/light_on.png"));
    }

#endif
}

void hw::on_btnFunc_Light3_clicked()
{
#if BOARD
    if(Api_Work_Get_CtrbState().s_light3==SWITCH_ON){
        Api_Work_CtrbCmd_Light3(CTRB_SWITCH_OFF);
        ui->btnFunc_Light1->setIcon(QIcon(":/icos/res/ico/light_off.png"));
    }
    else{
        Api_Work_CtrbCmd_Light3(CTRB_SWITCH_ON);
        ui->btnFunc_Light1->setIcon(QIcon(":/icos/res/ico/light_on.png"));
    }

#endif
}

void hw::on_btnFunc_Setting_clicked()
{

}

void hw::on_btnFunc_Aboutme_clicked()
{
    if(aboutme==NULL)
        aboutme=new AboutME(this);
    aboutme->show();
}

void hw::on_btnFunc_Log_clicked()
{

}

void hw::SystemClkUpdate()
{
    QDateTime dt;
    QString str= QString::number(dt.currentDateTime().date().year())
                +"/"+QString::number(dt.currentDateTime().date().month())
                +"/"+QString::number(dt.currentDateTime().date().day())
               +" "+QString::number(dt.currentDateTime().time().hour())
                                   +":"+QString::number(dt.currentDateTime().time().minute());
    ui->lblDate->setText(str);
}
